package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.FoodDao;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public List<Food> listFoodByBusinessId(Integer businessId)   throws Exception{
        String sql = "SELECT * FROM food WHERE businessId=? ORDER BY businessId";
        List<Food> list = new ArrayList<>();
            try {
                //Connection从ThreadLocal中获取
                con = DBUtil.getConnection();
                pst = con.prepareStatement(sql);
                //将？替换具体的值
                pst.setInt(1, businessId);
                //执行查询
                rs = pst.executeQuery();
                while (rs.next()){
                    Food food = new Food();
                    food.setFoodId(rs.getInt("foodId"));
                    food.setFoodName(rs.getString("foodName"));
                    food.setFoodExplain(rs.getString("foodExplain"));
                    food.setFoodImg(rs.getString("foodImg"));
                    food.setFoodPrice(rs.getDouble("foodPrice"));
                    food.setBusinessId(rs.getInt("businessId"));
                    food.setRemarks(rs.getString("remarks"));
                    //将对象添加到list中
                    list.add(food);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return list;
        }
}
